import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {api, ignoreResp} from "db://assets/script/game/player/api";
import {pb} from "../../../network/ws/proto";
import {GameType} from "db://assets/script/game/constants/common";
import {RestoreDeskComp} from "db://assets/script/game/dating/comp/restoreDeskComp";
import {smc} from "db://assets/script/game/common/singletonModuleComp";

export class RestoreDeskSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(RestoreDeskComp);
    }

    entityEnter(entity: ecs.Entity) {
        const comp = entity.get(RestoreDeskComp)
        // 删除 comp 前，保存数据
        const reconnect = comp.reconnect
        // 还原麻将 desk
        const restoreMj = (resp: pb.IDeskQueryIncompleteDeskInfo, reconnect: boolean) => {
            // 之前掉线且在房间内
            smc.gameMj.addInitComp(resp.deskInfo, resp.playerList, resp.categoryConf, reconnect, resp.duanPai, null, resp.roundInfo)
        }
        // 存在房间
        const existDesk = (resp: pb.IDeskQueryIncompleteDeskInfo) => {
            switch (resp.gameType) {
                case GameType.mj:
                    restoreMj(resp, reconnect)
                    break
                default:
                    console.error("no such game", resp.gameType)
            }
        }
        api.deskQueryIncompleteDesk(null , existDesk, ignoreResp)
        smc.dating.remove(RestoreDeskComp)
    }
}